package com.sumainfor.pc.gmio.service.impl;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.gmio.dao.DiTenantBillMapper;
import com.sumainfor.pc.gmio.feign.BaseTenantFeign;
import com.sumainfor.pc.gmio.feign.Zfeign.ZpcEpcFeign;
import com.sumainfor.pc.gmio.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.gmio.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.gmio.feign.ZuulFeign.ZuulFeign;
import com.sumainfor.pc.gmio.feign.extend.HouseFeign;
import com.sumainfor.pc.gmio.service.DiTenantBillService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 账单1
 */
@Service
public class DiTenantBillSerivceImpl implements DiTenantBillService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(DiTenantBillSerivceImpl.class);
    @Autowired
    private DiTenantBillMapper diTenantBillMapper;
    @Autowired
    private ZpcMktFeign zpcMktFeign;
    @Autowired
    private ZzuulFeign zzuulFeign;

    @Autowired
    private BaseTenantFeign tenantFeign;

    @Autowired
    BaseTenantFeign BaseTenantFeign;

    @Autowired
    HouseFeign HouseFeign;

    @Autowired
    ZuulFeign ZuulFeign;

    @Autowired
    DiDeferredApplySerivceImpl DiDeferredApplySerivceImpl;

    @Autowired
    ZpcEpcFeign ZpcEpcFeign;

    @Autowired
    HoRechargeServiceimpl HoRechargeServiceimpl;

    /**
     * 查询账单列表
     *
     * @param params
     * @return
     */
    @Override
    public JsonResult getTenantBillList(Map<String, Object> params, Pager pager) {

        pager.setPageno(Integer.parseInt(params.get("pageno").toString()));
        pager.setPagesize(Integer.parseInt(params.get("pagesize").toString()));

        JsonResult result = new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        //只有水电燃账单才根据水电燃去查询相对应的水电燃
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"utilityFeeRadio1"})) {
            //如果水电燃查询，是全部，就清空，相对应查询全部
            if (params.get("utilityFeeRadio1").toString().equals("0")) {
                params.put("utilityFeeRadio1", null);
            }
            //如果不是水电燃类型。就清空水电燃字段
            if (!params.get("BiType").toString().equals("2")) {
                params.put("utilityFeeRadio1", null);
            }
        }

        Integer cout = new Integer(0);
        List<Map<String, Object>> list = new ArrayList<>();
        if (!FieldCheckUtil.isMapFieldNotNull(params, new String[]{"htp"})) {
            list = diTenantBillMapper.getTenantBillList(params);
            cout = this.getTenantBillListCount(params);
        } else {
            List<String> TenIdList = new ArrayList<>();
            JsonResult zwxTenant = BaseTenantFeign.getTenIdPhone(params);
            List<Map<String, Object>> zwxTenList = (List<Map<String, Object>>) zwxTenant.get("result");
            if (zwxTenList.size() > 0) {
                for (Map<String, Object> zczsMap : zwxTenList) {
                    TenIdList.add(MapUtils.getString(zczsMap, "TenId"));
                }
                params.put("TenIdList", TenIdList);
                list = diTenantBillMapper.getTenantBillList(params);
                cout = this.getTenantBillListCount(params);
            } else {
                list = diTenantBillMapper.getTenantBillList(params);
                cout = this.getTenantBillListCount(params);
            }

        }

        //  List<Map<String,Object>> list=diTenantBillMapper.getTenantBillList(params);
        if (list.size() > 0) {
            String BiType = params.get("BiType").toString();
            List<String> ids;
            //获取公司。类型为1
            Integer type = 1;
            //获取房源资料

            List<Map<String, Object>> getAllHousingLists = HouseFeign.getAllHousingLists();
            //租客资料
            JsonResult jsonResult = BaseTenantFeign.getTenantList();
            List<Map<String, Object>> getTenantList = (List<Map<String, Object>>) jsonResult.getResult();
            switch (BiType) {
                case "1"://房租账单
                    //合并租客资料
                    list = ListMapMergeUtil.ListMergeUtils(list, getTenantList, "TenId");
                    //合并房源
                    list = ListMapMergeUtil.ListMergeUtils(list, getAllHousingLists, "HouInfoId");

                    for (Map<String, Object> map : list) {
                        String CreateType = map.get("CreateType").toString();
                        //账单创建人类型
                        if (CreateType == "2" || CreateType.equals("2")) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("Create").toString());
                            JsonResult jsonResult1 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult1.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult1.getResult();
                                map.put("CreateUserName", getUserInfo.get(0).get("username"));
                            }
                        }

                        //账单审核人
                        if (FieldCheckUtil.isMapFieldNotNull(map, new String[]{"AuditId"})) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("AuditId").toString());
                            JsonResult jsonResult1 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult1.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult1.getResult();
                                map.put("AuditIdName", getUserInfo.get(0).get("username"));
                            }
                        }

                        //根据支付公司编号获取公司资料
                        if (StringUtils.isNotEmpty(map.get("CorId").toString())) {
                            //获取当前支付的公司资料
                            Map<String, Object> getDataByTypeAndId = ZuulFeign.getDataByTypeAndId(map.get("CorId").toString(), type);
                            map.putAll(getDataByTypeAndId);
                        }

                        //管家查询
                        if (FieldCheckUtil.isMapFieldNotNull(map, new String[]{"SysUserId"})) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("SysUserId").toString());
                            JsonResult jsonResult2 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult2.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult2.getResult();
                                map.put("SysUserName", getUserInfo.get(0).get("username"));
                            }
                        }
                    }
                    break;
                case "2"://公共事业费
                    //合并房源
                    list = ListMapMergeUtil.ListMergeUtils(list, getAllHousingLists, "HouInfoId");
                    //合并租客资料
                    list = ListMapMergeUtil.ListMergeUtils(list, getTenantList, "TenId");
                    for (Map<String, Object> map : list) {
                        String CreateType = map.get("CreateType").toString();
                        //账单创建人类型
                        if (CreateType == "2" || CreateType.equals("2")) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("Create").toString());
                            JsonResult jsonResult1 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult1.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult1.getResult();
                                map.put("CreateUserName", getUserInfo.get(0).get("username"));
                            }
                        }
                        //根据支付公司编号获取公司资料
                        if (StringUtils.isNotEmpty(map.get("CorId").toString())) {
                            //获取当前支付的公司资料
                            Map<String, Object> getDataByTypeAndId = ZuulFeign.getDataByTypeAndId(map.get("CorId").toString(), type);
                            map.putAll(getDataByTypeAndId);
                        }
                    }
                    break;
                case "3"://维修费用
                    //合并租客资料
                    list = ListMapMergeUtil.ListMergeUtils(list, getTenantList, "TenId");
                    //合并房源
                    list = ListMapMergeUtil.ListMergeUtils(list, getAllHousingLists, "HouId");
                    for (Map<String, Object> map : list) {
                        String CreateType = map.get("CreateType").toString();
                        //账单创建人类型
                        if (CreateType == "2" || CreateType.equals("2")) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("Create").toString());
                            JsonResult jsonResult1 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult1.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult1.getResult();
                                map.put("CreateUserName", getUserInfo.get(0).get("username"));
                            }
                        }

                        //根据支付公司编号获取公司资料
                        if (StringUtils.isNotEmpty(map.get("CorId").toString())) {
                            //获取当前支付的公司资料
                            Map<String, Object> getDataByTypeAndId = ZuulFeign.getDataByTypeAndId(map.get("CorId").toString(), type);
                            map.putAll(getDataByTypeAndId);
                        }

                        Map<String, Object> getBillByOrdBilIdMap = ZpcEpcFeign.getBillByOrdBilIdMap(map.get("Source").toString());

                        map.putAll(getBillByOrdBilIdMap);

                    }
                    break;
                case "4"://暂无

                    break;
                case "5"://其他费用

                    break;
                case "6"://退租费用
                    //合并租客资料
                    list = ListMapMergeUtil.ListMergeUtils(list, getTenantList, "TenId");
                    //合并房源
                    list = ListMapMergeUtil.ListMergeUtils(list, getAllHousingLists, "HouId");
                    for (Map<String, Object> map : list) {
                        String CreateType = map.get("CreateType").toString();
                        //账单创建人类型
                        if (CreateType == "2" || CreateType.equals("2")) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("Create").toString());
                            JsonResult jsonResult1 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult1.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult1.getResult();
                                map.put("CreateUserName", getUserInfo.get(0).get("username"));
                            }
                        }

                        //根据支付公司编号获取公司资料
                        if (StringUtils.isNotEmpty(map.get("CorId").toString())) {
                            //获取当前支付的公司资料
                            Map<String, Object> getDataByTypeAndId = ZuulFeign.getDataByTypeAndId(map.get("CorId").toString(), type);
                            map.putAll(getDataByTypeAndId);
                        }

                        //退租发起人

                        if (MapUtils.getString(map, "LauId") != null) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("LauId").toString());
                            JsonResult lau = ZuulFeign.getUserInfo(ids);
                            if (lau.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) lau.getResult();
                                map.put("LauUserName", getUserInfo.get(0).get("username"));
                            }
                        }
                        //退租审核人

                        if (MapUtils.getString(map, "WitId") != null) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("WitId").toString());
                            JsonResult wit = ZuulFeign.getUserInfo(ids);
                            if (wit.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) wit.getResult();
                                map.put("WitUserName", getUserInfo.get(0).get("username"));
                            }
                        }

                    }
                    break;
                case "7"://夜间开门费用
                    //合并租客资料
                    list = ListMapMergeUtil.ListMergeUtils(list, getTenantList, "TenId");
                    //合并房源
                    list = ListMapMergeUtil.ListMergeUtils(list, getAllHousingLists, "HouId");
                    for (Map<String, Object> map : list) {
                        String CreateType = map.get("CreateType").toString();
                        //账单创建人类型
                        if (CreateType == "2" || CreateType.equals("2")) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("Create").toString());
                            JsonResult jsonResult1 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult1.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult1.getResult();
                                map.put("CreateUserName", getUserInfo.get(0).get("username"));
                            }
                        }

                        //根据支付公司编号获取公司资料
                        if (StringUtils.isNotEmpty(map.get("CorId").toString())) {
                            //获取当前支付的公司资料
                            Map<String, Object> getDataByTypeAndId = ZuulFeign.getDataByTypeAndId(map.get("CorId").toString(), type);
                            map.putAll(getDataByTypeAndId);
                        }
                    }
                    break;
                case "8"://智能电表充值
                    //合并租客资料
                    list = ListMapMergeUtil.ListMergeUtils(list, getTenantList, "TenId");
                    //合并房源
                    list = ListMapMergeUtil.ListMergeUtils(list, getAllHousingLists, "HouInfoId");
                    for (Map<String, Object> map : list) {
                        String CreateType = map.get("CreateType").toString();
                        //账单创建人类型
                        if (CreateType == "2" || CreateType.equals("2")) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("Create").toString());
                            JsonResult jsonResult1 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult1.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult1.getResult();
                                map.put("CreateUserName", getUserInfo.get(0).get("username"));
                            }
                        }

                        //根据支付公司编号获取公司资料
                        if (StringUtils.isNotEmpty(map.get("CorId").toString())) {
                            //获取当前支付的公司资料
                            Map<String, Object> getDataByTypeAndId = ZuulFeign.getDataByTypeAndId(map.get("CorId").toString(), type);
                            map.putAll(getDataByTypeAndId);
                        }

                        Map<String, Object> getReChargeIdByMap = HoRechargeServiceimpl.getReChargeIdByMap(map.get("Source").toString());
                        if (MapUtils.isNotEmpty(getReChargeIdByMap)) {
                            map.putAll(getReChargeIdByMap);
                        }

                    }
                    break;
                case "9"://续签合同总账单
                    //合并租客资料
                    list = ListMapMergeUtil.ListMergeUtils(list, getTenantList, "TenId");
                    //合并房源
                    list = ListMapMergeUtil.ListMergeUtils(list, getAllHousingLists, "HouId");
                    for (Map<String, Object> map : list) {
                        String CreateType = map.get("CreateType").toString();
                        //账单创建人类型
                        if (CreateType == "2" || CreateType.equals("2")) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("Create").toString());
                            JsonResult jsonResult1 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult1.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult1.getResult();
                                map.put("CreateUserName", getUserInfo.get(0).get("username"));
                            }
                        }

                        //根据支付公司编号获取公司资料
                        if (StringUtils.isNotEmpty(map.get("CorId").toString())) {
                            //获取当前支付的公司资料
                            Map<String, Object> getDataByTypeAndId = ZuulFeign.getDataByTypeAndId(map.get("CorId").toString(), type);
                            map.putAll(getDataByTypeAndId);
                        }

                        //
                        Map<String, Object> PcatInfoByPaIdAndTenIdMap = HouseFeign.getPcatInfoByPaIdAndTenIdMap(new HashMap<String, Object>() {{
                            put("TenId", map.get("TenId"));
                            put("PaId", map.get("Source"));
                        }});
                        log.info("PcatInfoByPaIdAndTenIdMap----", PcatInfoByPaIdAndTenIdMap);
                        if (!MapUtils.isEmpty(PcatInfoByPaIdAndTenIdMap)) {
                            map.putAll(PcatInfoByPaIdAndTenIdMap);
                        }

                    }
                    break;
                case "10":
                    //合并租客资料
                    list = ListMapMergeUtil.ListMergeUtils(list, getTenantList, "TenId");
                    //合并房源
                    list = ListMapMergeUtil.ListMergeUtils(list, getAllHousingLists, "HouInfoId");
                    for (Map<String, Object> map : list) {
                        String CreateType = map.get("CreateType").toString();
                        //账单创建人类型
                        if (CreateType == "2" || CreateType.equals("2")) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("Create").toString());
                            JsonResult jsonResult1 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult1.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult1.getResult();
                                map.put("CreateUserName", getUserInfo.get(0).get("username"));
                            }
                        }

                        //根据支付公司编号获取公司资料
                        if (StringUtils.isNotEmpty(map.get("CorId").toString())) {
                            //获取当前支付的公司资料
                            Map<String, Object> getDataByTypeAndId = ZuulFeign.getDataByTypeAndId(map.get("CorId").toString(), type);
                            map.putAll(getDataByTypeAndId);
                        }

                        //根据延期退编号获取延期退资料,并且根据合同编号获取合同资料
                        Map<String, Object> getDeferredByDefIdMap = DiDeferredApplySerivceImpl.getDeferredByDefIdMap(new HashMap<String, Object>() {{
                            put("PaId", map.get("Source"));
                            put("TenId", map.get("TenId"));
                        }});
                        map.putAll(getDeferredByDefIdMap);

                    }
                    break;
                //业主违约单
                case "11":
                    //合并房源
                    //合并房源
                    list = ListMapMergeUtil.ListMergeUtils(list, getAllHousingLists, "HouId");
                    for (Map<String, Object> map : list) {
                        String CreateType = map.get("CreateType").toString();
                        //账单创建人类型
                        if (CreateType == "2" || CreateType.equals("2")) {
                            ids = new ArrayList<String>();
                            ids.add(map.get("Create").toString());
                            JsonResult jsonResult1 = ZuulFeign.getUserInfo(ids);
                            if (jsonResult1.isSuccess()) {
                                List<Map<String, Object>> getUserInfo = (List<Map<String, Object>>) jsonResult1.getResult();
                                map.put("CreateUserName", getUserInfo.get(0).get("username"));
                            }
                        }
                        //根据支付公司编号获取公司资料
                        if (StringUtils.isNotEmpty(map.get("CorId").toString())) {
                            //获取当前支付的公司资料
                            Map<String, Object> getDataByTypeAndId = ZuulFeign.getDataByTypeAndId(map.get("CorId").toString(), type);
                            map.putAll(getDataByTypeAndId);
                        }
                    }
                    break;
            }
        }
        //  return list;

        if (cout == 0) {
            return pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, cout);
        } else {
            List<Map<String, Object>> getCommunityList = list;
            result = pageUtils.getJsonResult(getCommunityList, params, cout);
            return result;
        }
    }

    /**
     * 查询账单总数
     *
     * @param params
     * @return
     */
    @Override
    public Integer getTenantBillListCount(Map<String, Object> params) {
        return diTenantBillMapper.getTenantBillListCount(params);
    }

    /**
     * 通过客户编号查询账单列表
     */
    @Override
    public List<Map<String, Object>> getTenantBillListByTenId(Map<String, Object> params) {
        return diTenantBillMapper.getTenantBillListByTenId(params);
    }

    /**
     * 通过客户编号查询历史账单总数
     */
    @Override
    public Integer getTenantBillListCountByTenId(Map<String, Object> params) {
        return diTenantBillMapper.getTenantBillListCountByTenId(params);
    }

    /**
     * 逾期的租客的账单
     *
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> getDiTenantBillListByTenIdwd(Map<String, Object> params) {
        List<Map<String, Object>> list = diTenantBillMapper.getDiTenantBillListByTenIdwd(params);
        List<String> TenantList = new ArrayList<String>();
        for (Map<String, Object> map : list) {
            if (map.get("TenId") != null) {
                TenantList.add(map.get("TenId").toString());
            }
        }
        params.put("TenantList", TenantList);
        JsonResult marketResult = zpcMktFeign.getTenantListByTenId(params);
        System.out.println("市场部返回值:" + marketResult);
        if (marketResult.get("errorCode").equals("0")) {
            List<Map<String, Object>> mlist = (List<Map<String, Object>>) marketResult.get("result");
            for (Map<String, Object> giomap : list) {
                for (Map<String, Object> mktmap : mlist) {
                    if (MapUtils.getString(giomap, "TenId").equals(MapUtils.getString(mktmap, "TenId"))) {
                        giomap.putAll(mktmap);
                    }
                }
            }
        }
        return list;
    }

    /**
     * 查询账单总数
     *
     * @param params
     * @return
     */
    @Override
    public Integer getDiTenantBillListCountByTenIdwd(Map<String, Object> params) {
        return diTenantBillMapper.getDiTenantBillListCountByTenIdwd(params);
    }

    /**
     * 租后主管 账单审核 根据账单编号查询详情信息
     */
    @Override
    public JsonResult getDiTenantBillInfo(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (params.get("BiId") == null) {
            return result.putFailed("参数不正确");
        }
        Map<String, Object> map = diTenantBillMapper.getDiTenantBillInfo(params);
        if (map != null) {
            Map<String, Object> zuulMap = new HashMap<>();
            if (map.get("AuditId") != null) {
                zuulMap.put("user_id", map.get("AuditId"));
                //到网关查询账单审核人信息
                JsonResult zuulResult = zzuulFeign.getUserInfo(zuulMap);
                if (zuulResult.get("errorCode").equals("0")) {
                    Map<String, Object> userMap = (Map<String, Object>) zuulResult.get("result");
                    if (userMap != null) {
                        map.putAll(userMap);
                    }
                    result.put(map);
                } else {
                    return result.putFailed("服务器繁忙");
                }
            } else {
                result.put(map);
            }
        } else {
            return result.putFailed("服务器繁忙");
        }
        return result;
    }

    /**
     * 租后主管日账核对列表收入账单
     */
    @Override
    public JsonResult getDiBillInlCheckList(Map<String, Object> params, Pager pager) {
        pager.setPageno(Integer.parseInt(params.get("pageno").toString()));
        pager.setPagesize(Integer.parseInt(params.get("pagesize").toString()));
        JsonResult result = new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();

        //如果模糊搜索的字段有项目id, 用项目id和楼号室号去查询所有的房源id
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"ComId"})) {
            List<String> houseIdList = (List<String>) zpcMktFeign.getHouseIdListByBuildAndRoom(params);
            if (houseIdList != null && houseIdList.size() > 0) {
                params.put("houIdList", houseIdList);
            }
        }
        //如果模糊搜索的字段有模糊搜索字段, 则根据模糊搜索字段去查询所有的租客id列表
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"search"})) {
            List<String> tenIdList = tenantFeign.getTenIdListByLikePhoneOrName((String) params.get("search"));
            if (tenIdList != null && tenIdList.size() > 0) {
                params.put("tenIdList", tenIdList);
            }
        }

        Integer cout = diTenantBillMapper.getDiBillInlCheckListCount(params);
        if (cout == 0) {
            return pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, cout);
        }
        List<Map<String, Object>> getCommunityList = diTenantBillMapper.getDiBillInlCheckList(params);
        return pageUtils.getJsonResult(getCommunityList, params, cout);
    }

}
